<template>
  <div class="content">
    <div class="header">
      <h3 class="title">部门列表</h3>
      <el-button type="primary" @click="handleNewUserClick">新建部门</el-button>
    </div>
    <div class="table">
      <el-table :data="pageList" style="width: 100%" border>
        <el-table-column align="center" type="selection" width="50px" />
        <el-table-column
          align="center"
          type="index"
          label="序号"
          width="60px"
        />
        <el-table-column
          align="center"
          prop="name"
          label="部门名称"
          width="120px"
        />
        <el-table-column
          align="center"
          prop="leader"
          label="部门领导"
          width="120px"
        />
        <el-table-column
          align="center"
          prop="parentId"
          label="上一级部门"
          width="120px"
        />

        <el-table-column align="center" prop="createAt" label="创建时间">
          <template #default="scope">
            {{ scope.row.createAt }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="updateAt" label="更新时间">
          <template #default="scope">
            {{ scope.row.updateAt }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          fixed="right"
          label="操作"
          width="150px"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              size="small"
              text
              icon="Edit"
              @click="handleEditBtnClick(scope.row)"
              >编辑</el-button
            >
            <el-button
              link
              type="danger"
              size="small"
              text
              icon="Delete"
              @click="handleDeleteClick(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, defineEmits } from "vue"
import { formatUTC } from "@/utils/format"
import { storeToRefs } from "pinia"
import useSystemStore from "@/stores/main/system/system"
// 自定义事件
const emit = defineEmits(["newClick", "editClick"])
const systemStore = useSystemStore()
// 页码相关的逻辑
const currentPage = ref(1)
const pageSize = ref(10)
// systemStore.postUserListAction()
fetchPageListDate()
// 2.获取usersList数据,进行展示
const { pageList, pageTotalCount } = storeToRefs(systemStore)
function handleSizeChange() {
  fetchPageListDate()
}
function handleCurrentChange() {
  fetchPageListDate()
}
//定义函数发送网络请求
function fetchPageListDate(formData: any = {}) {
  //1获取size,offset
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size
  const pageInfo = { size, offset }
  const queryInfo = { ...pageInfo, ...formData }
  systemStore.postPageListAction("department", queryInfo)
}
// 删除数据
function handleDeleteClick(id: number) {
  systemStore.deletePageByIdAction("department", id)
  console.log(id)
}
// 编辑用户
function handleEditBtnClick(data: any) {
  emit("editClick", data)
  // console.log(data)
}
// 新建用户
function handleNewUserClick() {
  emit("newClick")
}
defineExpose({ fetchPageListDate })
</script>
<style lang="less" scoped>
.content {
  margin-top: 20px;
  padding: 20px;
  background-color: #fff;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 10px;

  .title {
    font-size: 22px;
  }
}

.table {
  :deep(.el-table__cell) {
    padding: 12px 0;
  }

  .el-button {
    margin-left: 0;
    padding: 5px 8px;
  }
}

.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
